svelte/store#readable JavaScript Examples

The following examples show how to use svelte/store#readable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: time.js    From ethernal with MIT License 6 votes vote down vote up
time = readable(new Date(), function start(set) {
  const interval = setInterval(() => {
    set(new Date());
  }, 1000);

  return function stop() {
    clearInterval(interval);
  };
})
Example #2
Source File: keyNavArray.js    From untab with MIT License 6 votes vote down vote up
/**
 * Up and Down Keyboard navigation over an array
 * @param {Array} items Items for navigation
 * @param {number} initial Initial selected index
 * @returns {Readable<number>} returns a readable for the active index
 */
export default function(items, initial = 0) {
  let activeIdx = initial;

  return readable(activeIdx, (set) => {
    const handleKeyNav = (e) => {
      switch (e.key) {
        case 'ArrowDown':
          e.preventDefault();
          activeIdx = (activeIdx + 1) % items.length;
          break;

        case 'ArrowUp':
          e.preventDefault();
          activeIdx = (items.length + activeIdx - 1) % items.length;
          break;

        default:
          break;
      }
      set(activeIdx);
    };

    window.addEventListener('keydown', handleKeyNav);

    return () => window.removeEventListener('keydown', handleKeyNav);
  });
}
Example #3
Source File: States.js    From svelte-simple-datatables with MIT License 6 votes vote down vote up
createId(identifier = null)
    {
        const id = identifier ?? 'ssd-' + (Math.random() + 1).toString(36).substring(5)
		const { subscribe } = readable(id)
		return {
			subscribe,
			get: () => { return id }
		}
    }
Example #4
Source File: store.js    From readr-coverages with MIT License 5 votes vote down vote up
rawData = readable([], async function start(set) {
  const data = await loadData()
  set(data)
})
Example #5
Source File: render.js    From kit with MIT License 5 votes vote down vote up
updated = {
	...readable(false),
	check: () => false
}