Skip to content

Instantly share code, notes, and snippets.

@chrispsn
Last active January 6, 2018 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrispsn/0e60e813ba6c3786bc87a64792123a27 to your computer and use it in GitHub Desktop.
Save chrispsn/0e60e813ba6c3786bc87a64792123a27 to your computer and use it in GitHub Desktop.
Mesh file structure (WIP).
// The below format takes the 'store each sheet in one file' approach.
//
// I suspect we will end up deciding having multiple sheets in one file would be bad practice
// (eg too much code to read in one file, and better coding style to keep things modular).
//
// A single-sheet-per-file approach would likely discourage inter-sheet references
// (as in, imports or requires), because of the overhead of having an extra import.
// What I *think* would happen instead is you'd have:
// - an 'assumptions' sheet which is full of hardcodes,
// - a 'calcs' sheet that contains only the assumptions it needs for its calcs, and
// - a 'master' sheet that assigns the 'calcs' sheet as the prototype of the 'assumptions' sheet.
//
// A 'single file per sheet' approach would not prevent:
// - something like Webpack bundling multiple sheets up for production,
// - the Mesh UI from having multiple sheets open (although need to think about
// how the app would save which sheets to pull in, or indeed if it even needs to save that info).
const table_proto = {
// Logic so you can work with an object of arrays like an array of objects.
// Applied to an object literal as its __proto__.
// We *may* move this into the sheet literal.
};
const sheet_name = {
// Using 'with' in getters allows us to use 'sheet' as our anchor point, not 'this';
// that's important for nested objects like tables which have their own 'this'
// and therefore would not be able to look at other variables within the sheet.
// Note you can't use 'sheet_name' as the anchor point if you want sheets
// to be used as prototypes for other sheets.
// Note it needs to be a getter (that is, 'sheet: this' wouldn't work).
get sheet() {return this},
a: 123,
// Undecided if Mesh will automatically insert a `with` block into getters like below.
// I am leaning towards yes because while it means we can't use strict mode,
// it really makes working with scope easier.
get b() {with (this) {return a}},
get c() {with (this) {return sheet.a}},
// We still have 'sheet' as our anchor if we need access to other variables.
get a_table() {with (this) {return {
__proto__: table_proto,
get test_sheet_as_anchor() {return sheet.c},
dates: [ /* ... */],
// User could consider adding a second 'with' here for easier column refs
get years() {return a_table.dates.map(d => d.getYear())},
event_names: [ /* ... */ ],
}}},
// Not sure if attachments should be:
// a) in the sheet object literal <-- leaning towards this, per below
// b) outside the sheet literal, but within the same file
// c) in a separate file entirely (better for non-Mesh users,
// but you'd probably want something similar to an 'all tests must pass' pre-version control hook
// to ensure the code and grid location defs don't go out of sync).
attachments: {
a: [0, 0],
b: [1, 0],
a_table: [0, 2]
},
}
// Testing our scoping works
console.log(sheet_name.b)
console.log(sheet_name.a_table.test_sheet_as_anchor)
console.log(sheet_name.a_table.years)
// Still lets you consume sheets as templates
const sheet_based_on_template = {
__proto__: sheet_name,
a: 321
}
console.log(sheet_based_on_template.b)
console.log(sheet_based_on_template.c)
console.log(sheet_based_on_template.a_table.test_sheet_as_anchor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment