svelte/store#writable JavaScript Examples

The following examples show how to use svelte/store#writable. 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: utils.js    From kit with MIT License 7 votes vote down vote up
/** @param {any} value */
export function notifiable_store(value) {
	const store = writable(value);
	let ready = true;

	function notify() {
		ready = true;
		store.update((val) => val);
	}

	/** @param {any} new_value */
	function set(new_value) {
		ready = false;
		store.set(new_value);
	}

	/** @param {(value: any) => void} run */
	function subscribe(run) {
		/** @type {any} */
		let old_value;
		return store.subscribe((new_value) => {
			if (old_value === undefined || (ready && new_value !== old_value)) {
				run((old_value = new_value));
			}
		});
	}

	return { notify, set, subscribe };
}
Example #2
Source File: cache.js    From ethernal with MIT License 6 votes vote down vote up
_characterBalances = writable({
  // object containing balances for each type of fungible tokens  // @TODO: break it down ?
  coins: null,
  keys: null,
  fire: null,
  water: null,
  earth: null,
  air: null,
  electricity: null,
})
Example #3
Source File: router.js    From tinro with MIT License 6 votes vote down vote up
function routerStore(){

    const {subscribe} = writable(location.get(), set => {
        location.start(set);
        let un = aClickListener(location.go)
        return ()=>{
            location.stop();
            un();
        }
    });

    return {
        subscribe,
        goto: location.go,
        params: getParams, /* DEPRECATED */
        meta: meta, /* DEPRECATED */
        useHashNavigation: s => location.mode(s ? MODES.HASH : MODES.HISTORY), /* DEPRECATED */
        mode: {
            hash: ()=>location.mode(MODES.HASH),
            history: ()=>location.mode(MODES.HISTORY),
            memory: ()=>location.mode(MODES.MEMORY),
        },
        base: location.base,
        location: location.methods()
    }
}
Example #4
Source File: file-store.js    From packager with Apache License 2.0 6 votes vote down vote up
writableFileStore = (key) => {
  let hasQueried = false;
  const store = writable(null, () => {
    const unsubscribe = store.subscribe((file) => {
      if (hasQueried) {
        set(key, file)
          .catch((err) => {
            console.warn(err);
          });
      }
    });
    return unsubscribe;
  });
  get(key)
    .then((value) => {
      hasQueried = true;
      if (value) {
        store.set(value);
      }
    });
  return store;
}
Example #5
Source File: fsm.js    From untab with MIT License 6 votes vote down vote up
/**
 * Creates a basic reactive FSM. Based on XState
 * @param {Object} machine object representing the FSM
 * @returns {Writable<string>} active state
 */
export default function(machine) {
  const { subscribe, set, update } = writable(machine.initial);

  return {
    subscribe,
    send: (transition, cb=() => {}) => update((state) => {
      const nextState = machine
        .states[state]
        .on?.[transition];

      if (!nextState) return state;
      if (typeof cb === 'function') cb(nextState);

      return nextState;
    }),
    reset: () => set(machine.initial),
  };
}
Example #6
Source File: stores.js    From mitm-play with GNU Affero General Public License v3.0 6 votes vote down vote up
logstore = writable({
  respHeader: {},
  response: '',
  headers: '',
  logid: '',
  title: '',
  path: '',
  url: '',
  ext: ''
})
Example #7
Source File: notification-store.js    From rally-core-addon with Mozilla Public License 2.0 6 votes vote down vote up
export function createNotificationStore() {
  const _notification = writable({ id: undefined});
  let timeout;

  function send({ message, type = "default", code }) {
		_notification.set({ id: id(), type, message, code });
  }

  function clear() {
    _notification.set({ id: undefined });
  }

  const notifications = derived(_notification, ($notification, set) => {
    // if there already was a notification, let's clear the timer
    // and reset it here.
    clearTimeout(timeout);
    set($notification);
		// if this is not the reset message, set the timer.
    if ($notification.id) {
      timeout = setTimeout(clear, NOTIFICATION_TIMEOUT);
    }
  })
  const { subscribe } = notifications;

  return {
    timeoutID: timeout,
    subscribe,
    send,
    clear: () => {
      clearTimeout(timeout);
      clear();
    },
  }
}
Example #8
Source File: utils.js    From kit with MIT License 6 votes vote down vote up
export function create_updated_store() {
	const { set, subscribe } = writable(false);

	const interval = +(
		/** @type {string} */ (import.meta.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL)
	);
	const initial = import.meta.env.VITE_SVELTEKIT_APP_VERSION;

	/** @type {NodeJS.Timeout} */
	let timeout;

	async function check() {
		if (import.meta.env.DEV || import.meta.env.SSR) return false;

		clearTimeout(timeout);

		if (interval) timeout = setTimeout(check, interval);

		const file = import.meta.env.VITE_SVELTEKIT_APP_VERSION_FILE;

		const res = await fetch(`${assets}/${file}`, {
			headers: {
				pragma: 'no-cache',
				'cache-control': 'no-cache'
			}
		});

		if (res.ok) {
			const { version } = await res.json();
			const updated = version !== initial;

			if (updated) {
				set(true);
				clearTimeout(timeout);
			}

			return updated;
		} else {
			throw new Error(`Version check failed: ${res.status}`);
		}
	}

	if (interval) timeout = setTimeout(check, interval);

	return {
		subscribe,
		check
	};
}
Example #9
Source File: Filters.js    From svelte-simple-datatables with MIT License 6 votes vote down vote up
createLocalFilters() 
    {
        const { subscribe, update } = writable([])
        return {
            subscribe, update,
            add: (key, value) => update(store => {
                const filter = {key: key, value: value} 
                store = store.filter(item => { return item.key !== key && item.value.length > 0 })
                store.push(filter)
                return store
            }),
            remove: () => update(store => store = [])
        }
    }
Example #10
Source File: cache.js    From ethernal with MIT License 5 votes vote down vote up
_characterId = writable(null)
Example #11
Source File: stores.js    From origin-dollar with MIT License 5 votes vote down vote up
constructor({ name, icon, address }) {
    this.name = name;
    this.icon = icon;
    this.address = address;
    this.holdings = _.object(
      CONTRACTS.filter((x) => x.isERC20).map((x) => [x.name, writable(0)])
    );
  }
Example #12
Source File: stores.js    From packager with Apache License 2.0 5 votes vote down vote up
error = writable(null)
Example #13
Source File: store.js    From untab with MIT License 5 votes vote down vote up
storedKeys = writable({})
Example #14
Source File: stores.js    From svite with MIT License 5 votes vote down vote up
export function getStore(id, initialValue) {
  return stores[id] || (stores[id] = writable(initialValue));
}
Example #15
Source File: stores.js    From ZzFXM with MIT License 5 votes vote down vote up
title = new writable('')
Example #16
Source File: KonstaStore.js    From konsta with MIT License 5 votes vote down vote up
KonstaStore = writable({
  theme: 'material',
  dark: true,
  touchRipple: true,
})
Example #17
Source File: location.js    From lnft with GNU Affero General Public License v3.0 5 votes vote down vote up
location = writable({
  current: undefined,
  previous: undefined,
})