Plugins → Examples
You always can check out source code of existing plugins, like:
Or any of core plugins.
const plugin = ({display}) => {
display({
id: 'my-id',
title: 'Loading'
})
fetchResult().then((result) => {
display({
id: 'my-id',
title: `Fetched result: ${result}`
})
});
}
module.exports = {
fn: plugin,
}
const icon = require('[path-to-icon]/icon.png');
const plugin = ({display}) => {
display({
icon,
title: 'Title',
subtitle: 'Subtitle'
});
}
module.exports = {
fn: plugin,
}
const plugin = (scope) => {
const match = scope.term.match(/^emoj\s(.+)/);
if (match) {
searchEmojis(match[1]).then(results => {
scope.display(results)
})
};
}
module.exports = {
name: 'Search emojis...'
fn: plugin,
keyword: 'emoj'
}
// Some data needed for your plugin
let data;
// Fetch some data only on app initialization
const initialize = () => {
fetchYourData().then(result => {
data = result
});
}
const plugin = (scope) => {
const results = search(data, scope.term);
scope.display(results);
}
module.exports = {
initialize: initialize,
fn: plugin
}
let data;
// Run some long-running initialization process in background
const initialize = (cb) => {
fetchYourData().then(cb);
// and re-fetch this information once in 1h
setInterval(() => {
initialize(cb);
}, 60 * 3600);
}
const onMessage = (results) => {
data = results;
}
const plugin = (scope) => {
const results = search(data, scope.term);
scope.display(results);
}
module.exports = {
initializeAsync: initialize,
onMessage: onMessage,
fn: plugin
}
const { memoize, search } = require('cerebro-tools');
const preprocessJson = require('./preprocessJson');
// Memoize your fetched data from external API
const fetchData = memoize(() => {
return fetch('http://api/url')
.then(response => response.json())
.then(preprocessJson)
});
const plugin = ({term, display}) => {
fetchData().then(data => {
const results = search(data, term, (el) => el.name);
display(term);
})
}
module.exports = {
fn: plugin
};