MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 67 lines 1.7 kB view raw
1const knownServerReferences = new WeakMap(); 2const FunctionBind = Function.prototype.bind; 3const ArraySlice = Array.prototype.slice; 4 5function defaultEncodeFormAction() { 6 return { name: '$ACTION_ID_demo', method: 'POST' }; 7} 8 9function registerBoundServerReference(reference, id) { 10 if (knownServerReferences.has(reference)) return; 11 12 knownServerReferences.set(reference, { 13 id, 14 originalBind: reference.bind, 15 bound: null, 16 }); 17 18 Object.defineProperties(reference, { 19 $$FORM_ACTION: { 20 value: defaultEncodeFormAction, 21 }, 22 bind: { 23 value: bind, 24 }, 25 }); 26} 27 28function bind() { 29 const referenceClosure = knownServerReferences.get(this); 30 if (!referenceClosure) return FunctionBind.apply(this, arguments); 31 32 const newFn = referenceClosure.originalBind.apply(this, arguments); 33 const args = ArraySlice.call(arguments, 1); 34 35 knownServerReferences.set(newFn, { 36 id: referenceClosure.id, 37 originalBind: newFn.bind, 38 bound: Promise.resolve(args), 39 }); 40 41 Object.defineProperties(newFn, { 42 $$FORM_ACTION: { 43 value: this.$$FORM_ACTION, 44 }, 45 bind: { 46 value: bind, 47 }, 48 }); 49 50 return newFn; 51} 52 53async function action(x) { 54 return x; 55} 56 57registerBoundServerReference(action, 'demo'); 58 59console.log(`action.bind.eq.custom:${action.bind === bind}`); 60console.log(`action.formAction.type:${typeof action.$$FORM_ACTION}`); 61 62const bound = action.bind(null, 1); 63 64console.log(`bound.type:${typeof bound}`); 65console.log(`bound.bind.eq.custom:${bound.bind === bind}`); 66console.log(`bound.formAction.type:${typeof bound.$$FORM_ACTION}`); 67console.log(`bound.formAction.name:${bound.$$FORM_ACTION ? bound.$$FORM_ACTION().name : 'missing'}`);