Created
October 7, 2025 16:16
-
-
Save jankapunkt/c4469f2e2fe70341b709c05abcd6fe23 to your computer and use it in GitHub Desktop.
MeteorJS Architecture helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Meteor } from 'meteor/meteor' | |
| /** | |
| * Helper to return a value only on server. | |
| * @param x {*} Value to return on server | |
| * @return {*|undefined} | |
| */ | |
| export const onServer = x => Meteor.isServer ? x : undefined | |
| /** | |
| * Helper to execute a function only on server and return its result. | |
| * @param fct {function} | |
| * @return {*|undefined} | |
| */ | |
| export const onServerExec = fct => Meteor.isServer ? fct() : undefined | |
| /** | |
| * Helper to return a value only on client. | |
| * @param x {*} Value to return on client | |
| * @return {*|undefined} | |
| */ | |
| export const onClient = x => Meteor.isClient ? x : undefined | |
| /** | |
| * Helper to execute a function only on client and return its result. | |
| * @param fct {function} | |
| * @return {*|undefined} | |
| */ | |
| export const onClientExec = fct => Meteor.isClient ? fct() : undefined | |
| /** | |
| * Helper to automatically execute a function and return its result. | |
| * @param fct {function} | |
| */ | |
| export const iife = fct => fct() | |
| /** | |
| * Isomorphic execution helper to easily write isomorphic code. | |
| * Executes and returns the result of `client` function on client, | |
| * and the result of `server` function on server. | |
| * | |
| * @param client {function=} Function to execute on client | |
| * @param server {function=} Function to execute on server | |
| * @return {*|null} | |
| */ | |
| export const isomorph = ({ client, server }) => { | |
| if (Meteor.isClient && client) { | |
| return client() | |
| } | |
| if (Meteor.isServer && server) { | |
| return server() | |
| } | |
| return null | |
| } | |
| /** | |
| * Throws an error if not executed on server. | |
| * @example | |
| * const myFunc = () => { | |
| * ensureServer() | |
| * // ...continue with server-only logic | |
| * } | |
| */ | |
| export const ensureServer = () => { | |
| if (!Meteor.isServer) throw new Error('Scope is expected to be server-only!') | |
| } | |
| /** | |
| * Throws an error if not executed on client. | |
| * @example | |
| * const myFunc = () => { | |
| * ensureClient() | |
| * // ...continue with client-only logic | |
| * } | |
| */ | |
| export const ensureClient = () => { | |
| if (!Meteor.isClient) throw new Error('Scope is expected to be client-only!') | |
| } | |
| /** | |
| * Wraps a function and logs a deprecation warning when it's called. | |
| * @param fn {function} | |
| * @param explicitName {string=} Optional explicit name for the function | |
| * @return {function(...[*]): *} wrapped function | |
| */ | |
| export const deprecate = (fn, explicitName) => { | |
| const name = explicitName || fn.name || 'function' | |
| return function (...args) { | |
| console.warn(`DEPRECATION WARNING: ${name} is deprecated and will be removed in future versions.`) | |
| return fn.apply(this, args) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment